package com.gx.smart.base;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewbinding.ViewBinding;

import com.blankj.utilcode.util.BarUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * 基础activity
 */

public abstract class BaseMVVMActivity<T extends ViewBinding, VM extends BaseViewModel> extends AppCompatActivity {

    protected VM viewModel;
    protected T binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initStatusBar();
        binding = onBindViewBinding(getLayoutInflater());
        setContentView(binding.getRoot());
        initViewModel();
        initView();
        initContent();
        initData();
        initObserver();
    }


    private void initViewModel() {
        viewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(getApplication())).get(onBindViewModel());
    }


    protected void initStatusBar() {
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT);
        BarUtils.setStatusBarLightMode(this, true);
    }


    protected void initView() {

    }

    protected void initContent() {

    }

    protected void initData() {

    }

    protected void initObserver() {

    }

    protected abstract Class<VM> onBindViewModel();

    protected abstract T onBindViewBinding(@NonNull LayoutInflater inflater);

    protected void registerEventBus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    protected void unregisterEventBus() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().removeAllStickyEvents();
            EventBus.getDefault().unregister(this);
        }
    }

}